package com.techburg.xbrl.client;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.TreeViewModel;
import com.techburg.xbrl.shared.XBRLDataBase.Company;
import com.techburg.xbrl.shared.XBRLDataBase.FileType;
import com.techburg.xbrl.shared.XBRLDataBase.XBRLReportFile;

public class XBRLDataTree implements TreeViewModel {
  
  private SelectionModel<XBRLReportFile> selectionModel;
  
  public XBRLDataTree(final SelectionModel<XBRLReportFile> selectionModel) {
    this.selectionModel = selectionModel;
  }

  public <T> NodeInfo<?> getNodeInfo(T value) {
    if (value == null) {
      ListDataProvider<Company> dataProvider = new ListDataProvider<Company>();
      for (int i = 0; i < 5; i++) {
        dataProvider.getList().add(new Company(String.valueOf(i), "Company-" + String.valueOf(i), null));
      }
      return new DefaultNodeInfo<Company>(dataProvider, new CompanyCell());
    } else if (value instanceof Company) {
        ListDataProvider<XBRLReportFile> dataProvider = new ListDataProvider<XBRLReportFile>();
        for (int i = 0; i < 6; i++) {
          dataProvider.getList().add(new XBRLReportFile(String.valueOf(i), "File-" + String.valueOf(i) + "." + FileType.values()[i].value(), FileType.values()[i].value()));
        }
        return new DefaultNodeInfo<XBRLReportFile>(dataProvider, new XBRLFileCell(), selectionModel, null);
    }
    String type = value.getClass().getName();
    throw new IllegalArgumentException("Unsupported object type: " + type);
  }
  
  public static class CompanyCell extends AbstractCell<Company> {

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context, Company company, SafeHtmlBuilder sb) {
      if (company != null) {
        sb.appendEscaped(company.getName());
      }
    }
  }
  
  public static class XBRLFileCell extends AbstractCell<XBRLReportFile> {

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context, XBRLReportFile file, SafeHtmlBuilder sb) {
      if (file != null) {
        sb.appendEscaped(file.getFileName());
      }
    }
    
  }

  public boolean isLeaf(Object value) {
    return value instanceof XBRLReportFile;
  }

}
